package cc.eddic.practice.concurrency.impl;

import cc.eddic.practice.concurrency.LogItem;
import cc.eddic.practice.concurrency.LogParser;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

@Slf4j
public class CompressedLogParserImpl implements LogParser {
    @Override
    public Map<String, List<LogItem>> parse(String folderName) {

        //String folderName = "D:\\nginx-logs\\2023-04";
        File dir = new File(folderName);

        List<File> allFileList = new ArrayList<>();
        if (!dir.exists()) {
            System.out.println("目录不存在");
            return null;
        }
        getAllFile(dir, allFileList);
        log.debug("该文件夹下共有" + allFileList.size() + "个文件");

        Map<String, List<LogItem>> result = new HashMap<>();
        CompressedLogItemParserImpl compressedLogItemParser = new CompressedLogItemParserImpl();

        long logItemNum = 0;
        for (File f : allFileList) {

            List<String> lineList =  getGZLines(f.getPath());

            List<LogItem> logItemsList = new ArrayList<>();

            for (String line : lineList) {
                Optional<LogItem> logItem = compressedLogItemParser.parse(line);
                logItem.ifPresent(logItemsList::add);
            }
            logItemNum = logItemNum + logItemsList.size();
            result.put(f.getPath(), logItemsList);

        }

        log.debug("{}   {}",result.keySet().size(),logItemNum);

        return result;
    }

    private List<String> getGZLines(String path) {

        try {
            InputStream in = new GZIPInputStream(new FileInputStream(path));

            Scanner sc = new Scanner(in);
            List<String> lines = new ArrayList<>();
            while (sc.hasNextLine()) {
                lines.add(sc.nextLine());
            }
            return lines;

        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }

    }

    public static void getAllFile(File fileInput, List<File> allFileList) {
        // 获取文件列表
        File[] fileList = fileInput.listFiles();
        assert fileList != null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                // 递归处理文件夹
                // 如果不想统计子文件夹则可以将下一行注释掉
                getAllFile(file, allFileList);
            } else {
                // 如果是文件则将其加入到文件数组中
                allFileList.add(file);
            }
        }
    }

}



